﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using CE.Global;
using CE.Provider;

public partial class Admin_home_refinance_AddNewHFVariableInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.SiteAdmin))
            {
                Response.Redirect("~/Admin/AdminSecureLogin.aspx");
            }
        }
        else
        {
            Response.Redirect("~/Admin/AdminSecureLogin.aspx");
        }

        if (!IsPostBack)
        {
            
            ddlMinLoanTenure.Items.Clear();
            for (int i = 1; i <= 10; i++)
            {
                ddlMinLoanTenure.Items.Add(i.ToString());
            }

            ddlMaxLoanTenure.Items.Clear();
            for (int i = 11; i <= 30; i++)
            {
                ddlMaxLoanTenure.Items.Add(i.ToString());
            }
            ddlMaxLoanTenure.Text = "30";

            ddlMinAgeOfApplicant.Items.Clear();
            for (int i = 18; i <= 30; i++)
            {
                ddlMinAgeOfApplicant.Items.Add(i.ToString());
            }
            ddlMinAgeOfApplicant.Text = "21";

            ddlMaxAgeOfApplicant.Items.Clear();
            for (int i = 31; i <= 70; i++)
            {
                ddlMaxAgeOfApplicant.Items.Add(i.ToString());
            }
            ddlMaxAgeOfApplicant.Text = "65";

            DataTable _dtCompany = General.GetComboItem("ProviderCompany", false);
            ddlProviderName.DataSource = _dtCompany;
            ddlProviderName.DataTextField = _dtCompany.Columns[0].ColumnName.ToString();
            ddlProviderName.DataValueField = _dtCompany.Columns[1].ColumnName.ToString();
            ddlProviderName.DataBind();

            ClearFields();
            if (Request.Params["hfid"] != null)
            {
                lblHFID.Text = Request.Params["hfid"].ToString();
                ShowHFVarInfo();
            }
        }
    }

    private void ShowHFVarInfo()
    {
        try
        {
            if (lblHFID.Text != string.Empty)
            {
                Int64 _hfid = Convert.ToInt64(lblHFID.Text);
                HFVariableInfo _hfvarinfo = new HFVariableInfo();
                HFVariableInfoDB _hfvarinfodb = new HFVariableInfoDB();
                _hfvarinfo = _hfvarinfodb.GetVariableInfo(_hfid);
                if (_hfvarinfo != null)
                {
                    ddlProviderName.Text = Convert.ToString(_hfvarinfo.HFProviderId);
                    txtProductName.Text = _hfvarinfo.ProductName;
                    ddlLoanPurpose.Text = Convert.ToString(_hfvarinfo.LoanPurpose);
                    ddlPropertyType.Text = Convert.ToString(_hfvarinfo.PropertyType);
                    ddlInterestRatePackage.Text = Convert.ToString(_hfvarinfo.InterestRateType);
                    ddlMinLoanTenure.Text = Convert.ToString(_hfvarinfo.MinLoanTenure);
                    ddlMaxLoanTenure.Text = Convert.ToString(_hfvarinfo.MaxLoanTenure);
                    ddlMinAgeOfApplicant.Text = Convert.ToString(_hfvarinfo.MinAgeApplicant);
                    ddlMaxAgeOfApplicant.Text = Convert.ToString(_hfvarinfo.MaxAgeApplicant);
                    ddlMaxMarginFin.Text = Convert.ToString(_hfvarinfo.MaxMarginFin);
                    ddlLockInPeriod.Text = Convert.ToString(_hfvarinfo.LockInPeriod);
                    txtFullRedPen.Text = _hfvarinfo.FullRedPen;
                    txtPartialRepayMinAmt.Text = Convert.ToString(_hfvarinfo.MinPartialRepay);
                    txtPartialRepayMaxAmt.Text = Convert.ToString(_hfvarinfo.MaxPartialRepay);
                    txtHomeFireIns.Text = _hfvarinfo.HomeFireIns;
                    txtLegalFeeSub.Text = _hfvarinfo.LegalFeeSubsidy;
                    ddlMRTARequire.Text = Convert.ToString(_hfvarinfo.RequireMRTA);

                    txtInsYear1.Text = Convert.ToString(_hfvarinfo.InterestY1);
                    ddlY1SiborPeriod.Text = Convert.ToString(_hfvarinfo.Y1Sibor);
                    ddlY1SiborSign.Text = Convert.ToString(_hfvarinfo.Y1SiborSign);

                    txtInsYear2.Text = Convert.ToString(_hfvarinfo.InterestY2);
                    ddlY2SiborPeriod.Text = Convert.ToString(_hfvarinfo.Y2Sibor);
                    ddlY2SiborSign.Text = Convert.ToString(_hfvarinfo.Y2SiborSign);

                    txtInsYear3.Text = Convert.ToString(_hfvarinfo.InterestY3);
                    ddlY1SiborPeriod.Text = Convert.ToString(_hfvarinfo.Y1Sibor);
                    ddlY1SiborSign.Text = Convert.ToString(_hfvarinfo.Y1SiborSign);

                    txtInsYear1.Text = Convert.ToString(_hfvarinfo.InterestY1);
                    ddlY3SiborPeriod.Text = Convert.ToString(_hfvarinfo.Y3Sibor);
                    ddlY3SiborSign.Text = Convert.ToString(_hfvarinfo.Y3SiborSign);

                    txtInsYear4.Text = Convert.ToString(_hfvarinfo.InterestY4);
                    ddlY4SiborPeriod.Text = Convert.ToString(_hfvarinfo.Y4Sibor);
                    ddlY4SiborSign.Text = Convert.ToString(_hfvarinfo.Y4SiborSign);

                    txtInsYear5.Text = Convert.ToString(_hfvarinfo.InterestY5);
                    ddlY5SiborPeriod.Text = Convert.ToString(_hfvarinfo.Y5Sibor);
                    ddlY5SiborSign.Text = Convert.ToString(_hfvarinfo.Y5SiborSign);

                    txtInsThereAfter.Text = Convert.ToString(_hfvarinfo.InterestThereAfter);
                    ddlTASiborPeriod.Text = Convert.ToString(_hfvarinfo.TASibor);
                    ddlTASiborSign.Text = Convert.ToString(_hfvarinfo.TASiborSign);
                }

                _hfvarinfo = null;
                _hfvarinfodb = null;
            }
        }
        catch (Exception Ex)
        {

        }
    }
    protected void btnHFVarInfoSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            HFVariableInfo _hfvarinfo = new HFVariableInfo();
            HFVariableInfoDB _hfvarinfodb = new HFVariableInfoDB();
            if (lblHFID.Text != string.Empty)
            {
                _hfvarinfo.HFVariableId = Convert.ToInt64(lblHFID.Text);
            }
            _hfvarinfo.HFProviderId = Convert.ToInt64(ddlProviderName.Text);
            _hfvarinfo.ProductName = Server.HtmlEncode(General.ClearScriptTag(txtProductName.Text));
            _hfvarinfo.LoanPurpose = Convert.ToInt16(ddlLoanPurpose.Text);
            _hfvarinfo.PropertyType = Convert.ToInt16(ddlPropertyType.Text);
            _hfvarinfo.InterestRateType = Convert.ToInt16(ddlInterestRatePackage.Text);
            _hfvarinfo.MinLoanTenure = Convert.ToInt32(ddlMinLoanTenure.Text);
            _hfvarinfo.MaxLoanTenure = Convert.ToInt32(ddlMaxLoanTenure.Text);
            _hfvarinfo.MinAgeApplicant = Convert.ToInt32(ddlMinAgeOfApplicant.Text);
            _hfvarinfo.MaxAgeApplicant = Convert.ToInt32(ddlMaxAgeOfApplicant.Text);
            _hfvarinfo.MaxMarginFin = Convert.ToInt32(ddlMaxMarginFin.Text);
            _hfvarinfo.LockInPeriod = Convert.ToInt32(ddlLockInPeriod.Text);
            _hfvarinfo.FullRedPen = txtFullRedPen.Text;
            _hfvarinfo.MinPartialRepay = Convert.ToSingle(txtPartialRepayMinAmt.Text);
            _hfvarinfo.MaxPartialRepay = Convert.ToSingle(txtPartialRepayMaxAmt.Text);
            _hfvarinfo.HomeFireIns = txtHomeFireIns.Text;
            _hfvarinfo.LegalFeeSubsidy = txtLegalFeeSub.Text;
            _hfvarinfo.RequireMRTA = Convert.ToBoolean(ddlMRTARequire.Text);

            _hfvarinfo.InterestY1 = Convert.ToSingle(txtInsYear1.Text);
            _hfvarinfo.Y1Sibor = Convert.ToInt32(ddlY1SiborPeriod.Text);
            _hfvarinfo.Y1SiborSign = Convert.ToChar(ddlY1SiborSign.Text);

            _hfvarinfo.InterestY2 = Convert.ToSingle(txtInsYear2.Text);
            _hfvarinfo.Y2Sibor = Convert.ToInt32(ddlY2SiborPeriod.Text);
            _hfvarinfo.Y2SiborSign = Convert.ToChar(ddlY2SiborSign.Text);

            _hfvarinfo.InterestY3 = Convert.ToSingle(txtInsYear3.Text);
            _hfvarinfo.Y1Sibor = Convert.ToInt32(ddlY1SiborPeriod.Text);
            _hfvarinfo.Y1SiborSign = Convert.ToChar(ddlY1SiborSign.Text);

            _hfvarinfo.InterestY1 = Convert.ToSingle(txtInsYear1.Text);
            _hfvarinfo.Y3Sibor = Convert.ToInt32(ddlY3SiborPeriod.Text);
            _hfvarinfo.Y3SiborSign = Convert.ToChar(ddlY3SiborSign.Text);

            _hfvarinfo.InterestY4 = Convert.ToSingle(txtInsYear4.Text);
            _hfvarinfo.Y4Sibor = Convert.ToInt32(ddlY4SiborPeriod.Text);
            _hfvarinfo.Y4SiborSign = Convert.ToChar(ddlY4SiborSign.Text);

            _hfvarinfo.InterestY5 = Convert.ToSingle(txtInsYear5.Text);
            _hfvarinfo.Y5Sibor = Convert.ToInt32(ddlY5SiborPeriod.Text);
            _hfvarinfo.Y5SiborSign = Convert.ToChar(ddlY5SiborSign.Text);

            _hfvarinfo.InterestThereAfter = Convert.ToSingle(txtInsThereAfter.Text);
            _hfvarinfo.TASibor = Convert.ToInt32(ddlTASiborPeriod.Text);
            _hfvarinfo.TASiborSign = Convert.ToChar(ddlTASiborSign.Text);

            _hfvarinfo.Status = true;
            _hfvarinfo.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
            _hfvarinfo.HFVariableId = _hfvarinfodb.Insert(_hfvarinfo);

            if (_hfvarinfo.HFVariableId > 0)
            {
                ClearFields();
                lblMessage.Text = "Saved successfully!";
            }
            else
            {
                lblMessage.Text = "Saving failed!";
            }
        }
    }

    private void ClearFields()
    {
        lblHFID.Text = "";
        txtProductName.Text = "";
        ddlLoanPurpose.Text = "3";
        ddlPropertyType.Text = "1";
        ddlInterestRatePackage.Text = "2";
        ddlMinLoanTenure.Text = "1";
        ddlMaxLoanTenure.Text = "30";
        ddlMinAgeOfApplicant.Text = "21";
        ddlMaxAgeOfApplicant.Text = "65";
        ddlMaxMarginFin.Text = "90";
        ddlLockInPeriod.Text = "1";
        txtFullRedPen.Text = "NONE";
        txtPartialRepayMaxAmt.Text= "0";
        txtPartialRepayMinAmt.Text = "0";
        txtHomeFireIns.Text = "NONE";
        txtLegalFeeSub.Text = "NONE";
        ddlMRTARequire.Text = "false";

        txtInsYear1.Text = "";
        ddlY1SiborPeriod.Text = "0";
        ddlY1SiborSign.Text = "N";

        txtInsYear2.Text = "";
        ddlY2SiborPeriod.Text = "0";
        ddlY2SiborSign.Text = "N";

        txtInsYear3.Text = "";
        ddlY3SiborPeriod.Text = "0";
        ddlY3SiborSign.Text = "N";

        txtInsYear4.Text = "";
        ddlY4SiborPeriod.Text = "0";
        ddlY4SiborSign.Text = "N";

        txtInsYear5.Text = "";
        ddlY5SiborPeriod.Text = "0";
        ddlY5SiborSign.Text = "N";

        txtInsThereAfter.Text = "";
        ddlTASiborPeriod.Text = "0";
        ddlTASiborSign.Text = "N";

        lblMessage.Text = "";
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        ClearFields();
    }
}
